<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>8_实现表单的全选和反选</title>
    <style>
        table {
            width: 500px;
            border: 1px solid black;
        }

        tr {
            border: 1px solid black;
        }

        td {
            text-align: center;
        }
    </style>
    <script src="./lib/angularjs.js"></script>
</head>

<body ng-app="myApp" ng-controller="myCtrl">
    <table>
        <tr>
            <th>
                <input type="checkbox" ng-model="status" ng-checked="status" ng-change="change()">
                <span ng-bind="status ? '全选' : '取消全选'"></span>
            </th>
            <th>中文名</th>
            <th>拼音</th>
        </tr>
        <tr ng-repeat="v in data">
            <td><input type="checkbox" ng-model="v.status" ng-change="itemChange()"></td>
            <td>{{v.name}}</td>
            <td>{{v.value}}</td>
        </tr>
    </table>
</body>

<script>
    const myApp = angular.module("myApp", [])
    myApp.controller("myCtrl", ["$scope", function ($scope) {
        $scope.status = false
        $scope.data = [{
            name: "北京",
            value: "beijing",
            status: false,
        }, {
            name: "上海",
            value: "shanghai",
            status: false,
        }, {
            name: "广州",
            value: "guangzhou",
            status: false,

        }]
        $scope.change = function () {
            if (this.status) {
                this.data = this.data.map((it) => ({ ...it, status: true }))
            } else {
                this.data = this.data.map((it) => ({ ...it, status: false }))
            }
        }
        $scope.itemChange = function () {
            $scope.status = $scope.data.every((it) => it.status)
        }
    }])
</script>

</html>